package com.huike.game.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.huike.game.R;
import com.huike.game.base.Bean20;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Game20FragmentTemFragment extends Fragment {

    private Context context;
    private List<Bean20> dataList;
    public Game20FragmentTemFragment(List<Bean20> dataList) {
        this.dataList = dataList;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View inflate = View.inflate(context, R.layout.game20_tem_fragment, null);

        TextView id = inflate.findViewById(R.id.tv_game20_fragment_tem);

        LineChart chart = inflate.findViewById(R.id.lc_game20_tem);

        chart.setDescription("");
        chart.setDrawGridBackground(false);
        Legend legend = chart.getLegend();
        legend.setEnabled(false);// 图例不显示


        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(true);

        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setLabelCount(5, false);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setLabelCount(5, false);
        rightAxis.setDrawGridLines(false);
        rightAxis.setEnabled(false);

        // set data
        chart.setData((LineData) generateDataLine(id));

        chart.animateX(750);

        return inflate;

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private LineData generateDataLine(TextView id) {

        ArrayList<Entry> e1 = new ArrayList<Entry>();
        int num = dataList.size() <= 20 ? dataList.size() : 20;

        List<Integer> integers = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            e1.add(new BarEntry(dataList.get(i).getAir(), i));
            integers.add(dataList.get(i).getAir());
        }

        Integer min = Collections.min(integers);
        Integer max = Collections.max(integers);

        id.setText("过去1分钟最高气温：" +max +" °C , 最低气温：" +min+"°C");

        LineDataSet d1 = new LineDataSet(e1, "");
        d1.setLineWidth(2.5f);
        d1.setCircleSize(4.5f);
        d1.setHighLightColor(Color.rgb(244, 117, 117));
        d1.setDrawValues(false);


        ArrayList<LineDataSet> sets = new ArrayList<LineDataSet>();
        sets.add(d1);

        LineData cd = new LineData(getMonths(), sets);

        return cd;
    }
    private ArrayList<String> getMonths() {

        ArrayList<String> m = new ArrayList<String>();

        SimpleDateFormat format = new SimpleDateFormat("mm:ss");

        int num = dataList.size() <= 20 ? dataList.size() : 20;

        for (int i = 0; i < num; i++) {
            Long time = dataList.get(i).getTime();
            String format1 = format.format(new Date(time));
            m.add(format1);
        }

        return m;
    }
}
